package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.VolleyResponse;
import com.isunland.managesystem.entity.EmployeeLoanContent;
import com.isunland.managesystem.entity.SuccessMessage;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.MyUtils;
import java.util.HashMap;
import org.json.JSONException;
import org.json.JSONObject;

public class AddManageReimburseListFragment extends ManageReimburseListDetailFragment {
  public static final String EXTRA_TRAVELREIMBURSE_CONTENT =
      "com.isunland.managesystem.ui.EXTRA_EXTRA_TRAVELREIMBURSE_CONTENT";
  private EmployeeLoanContent mEmployeeLoanContent;

  // 创建菜单
  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    mEmployeeLoanContent =
        (EmployeeLoanContent) getArguments().getSerializable(EXTRA_TRAVELREIMBURSE_CONTENT);
    LogUtil.i("AddManageReimburseListFragment",
        mEmployeeLoanContent.getReceiverName() == null ? "null" : "notNull");
    super.onCreateOptionsMenu(menu, inflater);
  }

  // 初始化控件
  protected void initData() {
    //			//连网获取交通工具类型和id
    //			tvtraftool.setOnClickListener(new OnClickListener() {
    //
    //				@Override
    //				public void onClick(View arg0) {DialogFragment dialog = null;
    //				FragmentManager fm = getActivity().getSupportFragmentManager();
    //
    //					dialog = new TrafToolDialogFragment();
    //
    //				dialog.setTargetFragment(AddTravelReimburseListDetailFragment.this,
    //						TRAFTOOL);
    //				dialog.show(fm, "");
    //				}
    //			});
    // 详情
  }

  // 菜单点击事件
  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;
      case R.id.menu_item_confirm:
        boolean allEmpty = allEmpty();

        if (allEmpty) {
          Toast.makeText(getActivity(), R.string.nothing, Toast.LENGTH_SHORT).show();
        } else {
          volleyPostSave();
        }
        break;
      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  private void volleyPostSave() {

    String url = ApiConst.getAbsoluteUrl(ApiConst.URL_TRAVEL_LIST_SAVE);

    HashMap<String, String> params = new HashMap<String, String>();
    HashMap<String, String> paramsItem = new HashMap<String, String>();

    //			//类型和id是从主表传递过来的
    paramsItem.put("SUBDOCTYPE", mEmployeeLoanContent.getCertificateName());//单据明细类型
    paramsItem.put("SUBDOCTYPEID", mEmployeeLoanContent.getCertificateCode());//单据明细类型ID
    //出差人姓名和id
    paramsItem.put("TRAVELER", mEmployeeLoanContent.getReceiverName());//出差人
    paramsItem.put("TRAVELERID", mEmployeeLoanContent.getReceiverId());//出差人ID

    paramsItem.put("FEE_DATE", tvappendDate.getText().toString());

    // 合同
    if (!TextUtils.isEmpty(mfeeKindId)) {
      paramsItem.put("FEE_KIND", mfeeKindName);//费用分类
      paramsItem.put("FEE_KINDID", mfeeKindId);//费用分类id
      paramsItem.put("REIMBURSEMENT_WAY", mfeeKindLen);//费用分类len
      paramsItem.put("ADDITION_ITEM", mfeeKindFlag);//费用分类flag
    }

    paramsItem.put("FEE_AMOUNT", etfeeAmount.getText().toString());
    paramsItem.put("BILL_NUM", etbillNum.getText().toString());
    paramsItem.put("BILL_NO", etbillNo.getText().toString());
    paramsItem.put("FEE_NODE", etfeeNote.getText().toString());
    paramsItem.put("NOTE", etnote.getText().toString());

    String test = new JSONObject(paramsItem).toString();

    LogUtil.d(mEmployeeLoanContent.toString());
    params.put("json", test);
    params.put("regJobNo", mCurrentUser.getJobNumber());//申请人工号
    params.put("mainId", mEmployeeLoanContent.getId());//主表id
    LogUtil.i("管理明细参数==" + params.toString());
    MyUtils.showDialogLoading(getActivity());
    mActivity.volleyPost(url, params, new VolleyResponse() {

      @Override public void onVolleyResponse(String arg0) throws JSONException {
        // 解析响应的字符串
        SuccessMessage[] result = new Gson().fromJson(arg0, SuccessMessage[].class);
        SuccessMessage workProcess = result[0];

        if (null == workProcess || null == workProcess.getResult()) {
          Toast.makeText(getActivity(), R.string.failure_operation, Toast.LENGTH_SHORT).show();
          return;
        }
        String successType = workProcess.getResult();
        if (successType.equals(SuccessMessage.FAILURE)) {
          Toast.makeText(getActivity(), R.string.failure_operation, Toast.LENGTH_SHORT).show();
          return;
        }
        if (successType.equals(SuccessMessage.SUCCESS)) {
          Toast.makeText(getActivity(), R.string.success_operation, Toast.LENGTH_SHORT).show();
          getActivity().setResult(Activity.RESULT_OK);
          getActivity().finish();
        }
      }

      @Override public void onVolleyError(VolleyError arg0) {
        // Auto-generated method stub

      }
    });
  }

  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != Activity.RESULT_OK) return;

    super.onActivityResult(requestCode, resultCode, data);
  }

  public static AddManageReimburseListFragment newInstance(EmployeeLoanContent item) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_TRAVELREIMBURSE_CONTENT, item);
    AddManageReimburseListFragment fragment = new AddManageReimburseListFragment();
    fragment.setArguments(args);
    return fragment;
  }
}
